<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        type="danger"
        plain
        round
        size="mini"
        class="editBtn"
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid :column-num="4" :gutter="10">
      <van-grid-item
        v-for="(value, ind) in channels"
        :key="ind"
        class="grid-item"
        @click="fnA(value, ind)"
      >
        <van-icon
          slot="icon"
          name="clear"
          v-show="isEdit && !fiexChannel.includes(value.id)"
        ></van-icon>
        <div slot="text">
          <span :class="{ active: ind === active }">
            {{ value.name }}
          </span>
        </div>
      </van-grid-item>
    </van-grid>
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid :column-num="4" :gutter="10" class="deepGrid">
      <van-grid-item
        v-for="(value, ind) in getAllChannelsCP"
        :key="ind"
        icon="plus"
        :text="value.name"
        class="grid-item"
        @click="addChannel(value)"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannels,
  delUserChannels
} from '@/api/channel';
import { mapState } from 'vuex';
// import request from '../utils/request';
import { setItem } from '../utils/storage';
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    channels: {
      typeof: Array,
      required: true
    },
    active: {
      typeof: Number,
      required: true
    }
  },
  data () {
    return {
      newChannels: [],
      isEdit: false,
      fiexChannel: [0, 11]
    };
  },
  computed: {
    ...mapState(['user']),
    getAllChannelsCP () {
      const arr = [];
      this.newChannels.forEach((element) => {
        const ret = this.channels.find((el) => {
          return el.id === element.id;
        });
        if (!ret) {
          arr.push(element);
        }
      });
      return arr;
    }
  },
  watch: {},
  created () {
    this.getAllChannelsFn();
  },
  mounted () {},
  methods: {
    fnA (value, ind) {
      // console.log(value, ind);
      if (this.isEdit) {
        if (this.fiexChannel.includes(value.id)) {
          return;
        }
        // 如果是编辑状态，执行删除
        this.channels.splice(ind, 1);
        if (ind <= this.active) {
          this.$emit('update-active', this.active - 1, true);
        }
        this.deleteChannel(value);
      } else {
        // 非编辑状态，执行切换频道
        this.$emit('update-active', ind, false);
      }
    },
    async getAllChannelsFn () {
      // const arr = [];
      const res = await getAllChannels();
      this.newChannels = res.data.data.channels;
    },
    async addChannel (value) {
      this.channels.push(value);
      if (this.user) {
        // 已登录
        try {
          await addUserChannels({
            id: value.id,
            seq: this.channels.length
          });
        } catch (error) {
          this.$toast('保存失败，请稍后重试');
        }
      } else {
        // 未登录
        setItem('TOUTIAO_CHANNELS', this.channels);
      }
    },
    async deleteChannel (channel) {
      try {
        if (this.user) {
          // 已登录，将数据更新到线上
          await delUserChannels(channel.id);
        } else {
          // 未登录，将数据存储到本地
          setItem('TOUTIAO_CHANNELS', this.channels);
        }
      } catch (error) {
        this.$toast('操作失败，请稍后重试');
      }
    }
  }
};
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
  .editBtn {
    // padding: 0 30px;
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  .title-text {
    font-size: 32px;
    color: #333;
  }
  .grid-item {
    width: 160px;
    height: 86px;
    /deep/ .van-grid-item__content {
      white-space: nowrap;
      background-color: #f5f5f6;
      .van-grid-item__text {
        color: #222;
        font-size: 28px;
        margin-top: 0;
      }
      span {
        font-size: 28px;
        margin-top: 0;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }
  /deep/ .deepGrid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
/deep/ .grid-item {
  .van-icon-clear {
    position: absolute;
    top: -10px;
    right: -10px;
    font-size: 30px;
    color: #cacaca;
    z-index: 2;
  }
}
.active {
  color: red;
}
// .van-grid-item {
//   position: unset;
// }
</style>
